﻿using Utilities.IO;

namespace Utilities.Media.Containers.MpegAudio
{
    public enum MpegAudioVersionID : byte
    {
        MPEG25 = 0,
        MPEG2 = 2,
        MPEG1 = 3
    }
    public enum MpegAudioLayer : byte
    {
        LayerIII = 1,
        LayerII = 2,
        LayerI = 3
    }

    public class MpegAudioFrameHeader : BitStream
    {
        private static short[] BitratesV1L1 = { -1, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, -1 };
        private static short[] BitratesV1L2 = { -1, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, -1 };
        private static short[] BitratesV1L3 = { -1, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1 };
        private static short[] BitratesV2L1 = { -1, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, -1 };
        private static short[] BitratesV2L23 = { -1, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, -1 };

        private static int[] FrequenciesV1 = { 44100, 48000, 32000, -1 };
        private static int[] FrequenciesV2 = { 22050, 24000, 16000, -1 };
        private static int[] FrequenciesV25 = { 11050, 12000, 8000, -1 };

        public MpegAudioVersionID VersionID { get; protected set; }
        public MpegAudioLayer Layer { get; protected set; }
        public bool Protected { get; protected set; }
        public short Bitrate { get; protected set; }
        public int Frequency { get; protected set; }

        public MpegAudioFrameHeader(DataStream baseStream) : base(baseStream)
        {
            ReadBits16(11);
            VersionID = (MpegAudioVersionID)ReadBits8(2);
            Layer = (MpegAudioLayer)ReadBits8(2);
            Protected = ReadBit();

            byte bitrateIndex = ReadBits8(4);
            if (VersionID == MpegAudioVersionID.MPEG1 && Layer == MpegAudioLayer.LayerI)
                Bitrate = BitratesV1L1[bitrateIndex];
            else if (VersionID == MpegAudioVersionID.MPEG1 && Layer == MpegAudioLayer.LayerII)
                Bitrate = BitratesV1L2[bitrateIndex];
            else if (VersionID == MpegAudioVersionID.MPEG1 && Layer == MpegAudioLayer.LayerIII)
                Bitrate = BitratesV1L3[bitrateIndex];
            else if ((VersionID == MpegAudioVersionID.MPEG2 || VersionID == MpegAudioVersionID.MPEG25) && Layer == MpegAudioLayer.LayerI)
                Bitrate = BitratesV2L1[bitrateIndex];
            else if ((VersionID == MpegAudioVersionID.MPEG2 || VersionID == MpegAudioVersionID.MPEG25) && (Layer == MpegAudioLayer.LayerII || Layer == MpegAudioLayer.LayerIII))
                Bitrate = BitratesV2L23[bitrateIndex];

            byte frequencyIndex = ReadBits8(2);
            if (VersionID == MpegAudioVersionID.MPEG1)
                Frequency = FrequenciesV1[frequencyIndex];
            else if (VersionID == MpegAudioVersionID.MPEG2)
                Frequency = FrequenciesV2[frequencyIndex];
            else if (VersionID == MpegAudioVersionID.MPEG25)
                Frequency = FrequenciesV25[frequencyIndex];

            // Padding bit
        }
    }
}
